<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-26
 * Time: 下午1:39
 * To change this template use File | Settings | File Templates.
 */
class tu_cc extends MY_Model
{
    private $domain = 'http://bt.2tu.cc/';
    private $flag = '2tu_cc';

    private $data = array(
        'china_name' => '', //中文名
        'english_name' => '',
        'class' => '', //分类 1电影 2剧集 3综艺 4动漫 5音乐 6游戏  7图书 8软件 9资料
        'director' => '', //导演
        'starring' => '', //主演
        'area' => '', //区域
        'type' => '', //影片类型
        'update_status' => '', //更新状态 1最新 2连载 3完结
        'public_time' => '', //上映时间
        'version' => '', //版本 1 dvd 2高清 3抢鲜 4vcd
        'language' => '', //语言  1中文  2英语  3双语 4其他
        'poster_addr' => '', //海报地址
        'drama' => '', //剧情介绍
        'episode' => '', //集数
        'dub' => '', //配音
        'status' => '', //1正常  0删除
        'uniqid' => '', //唯一值
        'img_addr' => '',            //图片地址
        'source' => '', //影片来源
        'source_id' => '', //影片在来源网站的id号
        'score' => '', //评分
        'subtitle' => '', //字幕
        'release' => '', //资源发布者
        'update_time' => '',//更新时间
        'create_time' => '', //创建时间
    );

    private $downlink = array();

    function __construct()
    {
        parent::__construct();
        //$this->load->helper('dom');
    }

    private function init()
    {
        foreach($this->data as $k=>$v)
        {
            $this->data[$k] = '';
        }

        $this->downlink = array();
        //$this->data['create_time'] = date('Y-m-d H:i:s');
    }

    public function analysis(array $arr)
    {
        $this->init();
        $html = $this->getFileContent($arr['file_name']);

        if (empty ($html)) return false;

        $this->data['status'] = 1;
        $this->data['source'] = $this->flag;
        $this->data['source_id'] = $arr['source_id'];
        $this->data['uniqid'] = md5($this->data['source'].$this->data['source_id']);
        $this->data['update_time'] = date('Y-m-d H:i:s');


        //影片类型
        preg_match("/当前位置: (.*)<\/div>/sU", $html, $tmp);

        if(isset($tmp[1]))
        {
            preg_match_all("/<a.*>(.*)<\/a>/sU", $tmp[1], $arrs);

            isset($arrs[1][1]) &&  $this->data['class'] = $this->getClass($arrs[1][1]);
            //isset($arrs[1][2]) &&  $this->data['type'] = implode('|', explode('  ', $arrs[1][2]));
            isset($arrs[1][2]) &&  $this->data['type'] = $arrs[1][2];
        }

        //影片名称
        //<!--h3>尸骨无存</h3-->
        $arrs = array();
        //preg_match_all("/<!--h3>(.*)<\/h3-->/", $html, $arrs, PREG_PATTERN_ORDER);
        preg_match("/<div id=\"playinfo\">.*<div class=\"img\"><img.*alt=\"(.*)\".*>/sU", $html, $names);
        isset($names[1]) &&  $this->data['china_name'] = $names[1];

        //更新状态
        $arrs = array();
        preg_match_all("/状态：(.*)<\/b>/sU", $html, $arrs, PREG_PATTERN_ORDER);
        isset($arrs[1][0]) &&  $this->data['update_status'] = strip_tags($arrs[1][0]);

        //更新时间
        $arrs = array();
        preg_match_all('/更新时间：(.*)<\/b>/', $html, $arrs, PREG_PATTERN_ORDER);
        isset($arrs[1][0]) &&  $this->data['create_time'] = $arrs[1][0];

        //地区
        $arrs = array();
        preg_match_all('/<b>地区：(.*)<\/b>/sU', $html, $arrs, PREG_PATTERN_ORDER);
        isset($arrs[1][0]) &&  $this->data['area'] = implode('|', explode('  ', $arrs[1][0]));

        //上映年代
        $arrs = array();
        preg_match_all('/<b>上映年代：(.*)<\/b>/', $html, $arrs, PREG_PATTERN_ORDER);
        isset($arrs[1][0]) &&  $this->data['public_time'] = $arrs[1][0];
        //上映年代
        $arrs = array();
        preg_match_all('/<p>主演：(.*)<\/p>/', $html, $arrs, PREG_PATTERN_ORDER);
        //print_r($arrs);
        isset($arrs[1][0]) &&  $this->data['starring'] = strip_tags(implode('|', explode('&nbsp;&nbsp;', $arrs[1][0])));

        //影片图片
        //<dt><a class="pic"><img src="http://tu.4567.tv/newpic/1.jpg" alt="尸骨无存" /><i></i></a></dt>
        $arrs = array();
        preg_match_all('/<div id=\"playinfo\">.*<div class=\"img\"><img.*src=\"(.*)\".*>/sU', $html, $arrs, PREG_PATTERN_ORDER);
        //print_r($arrs);
        isset($arrs[1][0]) &&  $this->data['img_addr'] = strip_tags($arrs[1][0]);

        //剧情介绍
        $arrs = array();
        preg_match_all('/剧情介绍.*<div class=\"about_t\">.*<p>(.*)<\/p>/sU', $html, $arrs, PREG_PATTERN_ORDER);
        //print_r($arrs);
        isset($arrs[1][0]) &&  $this->data['drama'] = strip_tags($arrs[1][0]);
        //echo $html;
        //下载地址
        $arrs = array();
        //$html = '<div class="mline"><span class="mtitle">第01集</span><span class="msize">151.54Mb</span><span class="mxunlei"><a title="第01集" href="http://www.2tu.cc/play/play13000.html?13000-0-0" target="_blank">迅播观看</a></span><span class="mkankan"><a title="第01集" href="javascript:;" onclick="start(\'gvod://vip.xubo.cc:8089/F99F7E12BA612C2031F1B40793B5EA3B44526D65/158900508/[迅雷下载www.2tu.cc]少女爱上姐姐2[第1话].mp4\')">迅雷看看</a></span><span class="mdown"><input name="xcheckbox" value="cid=F99F7E12BA612C2031F1B40793B5EA3B44526D65&amp;mc=[迅雷下载www.2tu.cc]少女爱上姐姐2[第1话].mp4" type="checkbox"><a href="javascript:;" onclick="return OnDownloadClick_Simple(this,1)" oncontextmenu="return ThunderNetwork_SetHref(this)">迅雷下载</a></span></div><div class="mline"><span class="mtitle">第02集</span><span class="msize">80.08Mb</span><span class="mxunlei"><a title="第02集" href="http://www.2tu.cc/play/play13000.html?13000-0-1" target="_blank">迅播观看</a></span><span class="mkankan"><a title="第02集" href="javascript:;" onclick="start(\'gvod://vip.xubo.cc:8089/8777F88D6CB6CF4991B83B24E7CE57E1E927DA14/83974494/[迅雷下载www.2tu.cc]少女爱上姐姐2[第2话].mp4\')">迅雷看看</a></span><span class="mdown"><input name="xcheckbox" value="cid=8777F88D6CB6CF4991B83B24E7CE57E1E927DA14&amp;mc=[迅雷下载www.2tu.cc]少女爱上姐姐2[第2话].mp4" type="checkbox"><a href="javascript:;" onclick="return OnDownloadClick_Simple(this,1)" oncontextmenu="return ThunderNetwork_SetHref(this)">迅雷下载</a></span></div>';
        //<div class='mline'><span class='mtitle'>第01集</span><span class='msize'>255.17Mb</span><span class='mxunlei'><a title='第01集' href='/play/play12838.html?12838-0-0' target="_blank">迅播观看</a></span><span class='mkankan'><a title='第01集' href="javascript:;" onclick="start('gvod://xlc.xunbo.cc:8089/437F7AD9FE20080E4017FD88C610226112FCEC50/267568659/[迅雷下载www.2tu.cc]Rich.Man.Poor.Woman.EP01.rmvb')">迅雷看看</a></span><span class='mdown'><input type='checkbox' name='xcheckbox' value='cid=437F7AD9FE20080E4017FD88C610226112FCEC50&mc=[迅雷下载www.2tu.cc]Rich.Man.Poor.Woman.EP01.rmvb'/><a href='javascript:;' onclick='return OnDownloadClick_Simple(this,1)'  oncontextmenu='return ThunderNetwork_SetHref(this)'>迅雷下载</a></span></div>
        preg_match("/<div class=\"hddown\">(.*)<\/div>/sU", $html, $tmps);
        preg_match_all("/<dl.*>.*<dt>(.*)<\/dt>.*<dd class=[\'|\"]s[\'|\"]>(.*)<\/dd>.*<input.*value=[\'|\"](.*)[\'|\"].*>/sU", $tmps[0], $arrs);
        //echo '<pre>';print_r($arrs);echo $html;exit;
        /*
        preg_match_all('/<div class=(?:\'|")mline(?:\'|")><span class=(?:\'|")mtitle(?:\'|")>(.*?)<\/span><span class=(?:\'|")msize(?:\'|")>(.*?)<\/span>.*?<input.*?value=(?:\'|")(.*?)(?:\'|").*?>/s', $html, $arrs, PREG_SET_ORDER);
        //*/

        foreach($arrs[1] as $k=>$item)
        {
            //list($tmp, $t['name'], $t['size'], $t['download_addr']) = $item;
            $t['name'] = $item;
            $t['size'] = $arrs[2][$k];
            $t['download_addr'] = $arrs[3][$k];

            $t['source_download_link'] = $this->domain.$t['download_addr'].'/'.$t['name'];
            $t['download_addr'] = getDownLoadUrl(htmlspecialchars_decode($t['download_addr']), $this->domain);
            $t['create_time'] = date('Y-m-d H:i:s');
            $this->downlink[] = $t;
        }

        if (empty ($this->downlink)) {
            //echo '<pre>';print_r($this->data);print_r($this->downlink);echo $html;exit;
        }

        //echo '<pre>';print_r($this->data);echo $html;exit;
        //echo '<pre>';print_r($this->data);print_r($this->downlink);echo $html;exit;
        return ;//echo $html;
    }

    /**
     * 保存数据
     *
     * @return mixed
     */
    public function save()
    {
        $this->db->insert('movie', $this->data, true);
        $movie_id = $this->db->insert_id();
        if ($movie_id) {
            $this->categoryCorrespond($movie_id, $this->data['class'], $this->data['type']);
        }

        if (!$movie_id) {
            $this->load->model('movie');

            $data = $this->movie->getMovieByChinaName($this->data['china_name']);
            if (!empty ($data)) {
                $movie_id = $data['id'];
            }
        }

        if($movie_id)
        {
            foreach($this->downlink as $key=>$item)
            {
                //md5($mId.$v['name'].$downloadLink.$v['size']),
                $this->downlink[$key]['video_id'] = $movie_id;
                $this->downlink[$key]['uniqueid'] = md5($movie_id.$this->downlink[$key]['name'].$this->downlink[$key]['download_addr'].$this->downlink[$key]['size']);
                //echo '<pre>';print_r($this->downlink);exit;
                $this->db->insert('download_list', $this->downlink[$key], true);
                $downloadId = $this->db->insert_id();
                if ($downloadId) {
                    $this->updateResource($movie_id);
                }
            }
        }
        //$this->db->insert_batch('download_list', $this->downlink);
        return;
    }
}
